<template>
  <div class="group_dialog" ref="group_dialog">
    <el-dialog
      :visible.sync="propData.briefDialogState"
      width="397px"
      :show-close="false"
      :close-on-click-modal="false">

      <div class="dialog_title">群简介</div>
      <div class="input_box">
        <textarea class="input_text" maxlength="500" v-model="briefText" placeholder="请填写群简介"></textarea>
        <div class="input_num">{{briefText.length}}/500</div>
      </div>
      <div class="btns_box">
        <div class="primarybtn default" @click="cancelDialog">取消</div>
        <div class="primarybtn" @click="saveBrief">保存</div>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="propData.noticeDialogState"
      width="397px"
      :show-close="false"
      :close-on-click-modal="false">

      <div class="dialog_title">群公告</div>
      <div class="input_box">
        <textarea class="input_text" maxlength="500" v-model="noticeText" placeholder="请填写群公告"></textarea>
        <div class="input_num">{{noticeText.length}}/500</div>
      </div>
      <div class="btns_box">
        <div class="primarybtn default" @click="cancelDialog">取消</div>
        <div class="primarybtn" @click="saveNotice">保存</div>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="propData.nickDialogState"
      width="290px"
      :show-close="false"
      :close-on-click-modal="false">

      <div class="dialog_title">我的群昵称</div>
      <div class="input_box single_line_box">
        <input class="input_text single_line_input" maxlength="30" v-model="nickText" placeholder="请填写群昵称" />
        <div class="input_num">{{nickText.length}}/30</div>
      </div>
      <div class="btns_box">
        <div class="primarybtn default" @click="cancelDialog">取消</div>
        <div class="primarybtn" @click="saveGroupName">保存</div>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="propData.banDialogState"
      width="290px"
      :show-close="false"
      :close-on-click-modal="false">

      <div class="dialog_title">禁言时长</div>
      <ul class="time_box">
        <li class="ban_time">
          <div class="time_duration">10分钟</div>
          <img class="choose_icon" src="../../assets/choose.png" />
        </li>
        <li class="ban_time">
          <div class="time_duration">1小时</div>
          <img class="choose_icon" src="../../assets/choose.png" />
        </li>
        <li class="ban_time">
          <div class="time_duration">24小时</div>
          <img class="choose_icon" src="../../assets/choose.png" />
        </li>
        <li class="ban_time">
          <div class="time_duration">永久禁言</div>
          <img class="choose_icon" src="../../assets/choose.png" />
        </li>
      </ul>
      <div class="btns_box" style="margin-top:10px;">
        <div class="primarybtn default" @click="cancelDialog">取消</div>
        <div class="primarybtn">确定</div>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="propData.deleteDialogState"
      width="290px"
      :show-close="false"
      :close-on-click-modal="false">

      <div class="dialog_tips">确定将该用户移出群聊？</div>
      <div class="btns_box">
        <div class="primarybtn default" @click="cancelDialog">取消</div>
        <div class="primarybtn" @click="deleteGroupMember">删除</div>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="propData.quitDialogState"
      width="290px"
      :show-close="false"
      :close-on-click-modal="false">

      <div class="dialog_tips">
        <div>确定退出当前群聊？</div>
        <div class="little_tips">退出后将不再接收此群消息</div>
      </div>
      <div class="btns_box">
        <div class="primarybtn default" @click="cancelDialog">取消</div>
        <div class="primarybtn" @click="exitGroup">确定</div>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="propData.breakDialogState"
      width="290px"
      :show-close="false"
      :close-on-click-modal="false">

      <div class="dialog_tips">
        <div>确定解散群聊吗？</div>
        <div class="little_tips">解散后，所有与群有关的记录都将被删除</div>
      </div>
      <div class="btns_box">
        <div class="primarybtn default" @click="cancelDialog">取消</div>
        <div class="primarybtn" @click="dismissGroup">确定</div>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="propData.overDialogState"
      width="342px"
      :show-close="false"
      :close-on-click-modal="false"
      :modal-append-to-body="false">

      <el-dialog
        :visible.sync="overInnerDialog"
        width="290px"
        :show-close="false"
        :close-on-click-modal="false"
        append-to-body>

        <div class="dialog_tips">
          <div>确定选择"<span style="color:#428dfc;">爱因斯坦</span>"为新群主？</div>
          <div class="little_tips">您将自动转为普通成员</div>
        </div>
        <div class="btns_box">
          <div class="primarybtn default" @click="cancelOverInnerDialog">取消</div>
          <div class="primarybtn">确定</div>
        </div>
      </el-dialog>

      <div class="over_group_top">
        <div class="over_group_title">选择新群主</div>
        <img class="close" src="../../assets/choose.png" @click="cancelDialog"/>
      </div>
      <div class="over_group_search">
        <img class="search_icon" src="../../assets/choose.png" />
        <input class="search_input" type="text" placeholder="请输入昵称"/>
      </div>

      <ul class="group_members_box">
        <li class="group_members_line" @click="openOverInnerDialog">
          <img class="group_member_avatar" src="../../assets/test_avatar.jpg" />
          <span class="group_member_name">测试</span>
        </li>
        <li class="group_members_line" @click="openOverInnerDialog">
          <img class="group_member_avatar" src="../../assets/test_avatar.jpg" />
          <span class="group_member_name">测试</span>
        </li>
      </ul>
    </el-dialog>

    <el-dialog
        :visible.sync="propData.banMembersDialogState"
        width="560px"
        :show-close="false"
        :close-on-click-modal="false">

        <el-dialog
          :visible.sync="removeBanDialog"
          width="290px"
          :show-close="false"
          :close-on-click-modal="false"
          append-to-body>

          <div class="dialog_tips">确定将{{banItem.userName}}移除禁言名单？</div>
          <div class="btns_box">
            <div class="primarybtn default" @click="cancelRemoveBanDialog">取消</div>
            <div class="primarybtn" @click="removeBan">确定</div>
          </div>
        </el-dialog>

        <div class="dialog_top">
            <div class="dialog_title">禁言名单</div>
            <img class="cancel_dialog_btn" src="../../assets/icons/icon_close_up.png" @click="cancelDialog" />
        </div>
        <div class="user_search">
            <div class="search_content">
                <img class="search_icon" src="../../assets/icons/icon_search.png"/>
                <input class="search_input" type="text" placeholder="输入昵称" maxlength="30" v-model="userText"/>
            </div>
            <div class="primarybtn" @click="getForbidSpeechList">查找</div>
        </div>
        
        <ul class="user_list">
            <li class="user_box" v-for="(item,index) in banList" :key="index">
                <img class="user_avatar" :src="item.userHeadImg" />
                <div class="user_text">
                    <div class="user_nick">{{item.userName}}</div>
                </div>
                <div class="remove_ban" @click="openRemoveBanDialog(item)">
                  <div class="remove_time">禁言中</div>
                  <img class="remove_icon" src="../../assets/remove.png"/>
                </div>
            </li>
        </ul>
    </el-dialog>

    <el-dialog
      :visible.sync="propData.removeBanDialogState"
      width="290px"
      :show-close="false"
      :close-on-click-modal="false">

      <div class="dialog_tips">确定将爱因斯坦移除禁言名单？</div>
      <div class="btns_box">
        <div class="primarybtn default" @click="cancelDialog">取消</div>
        <div class="primarybtn">确定</div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {Dialog,Message} from 'element-ui'
import bus from '../../bus'
import {upNoticeRemarks,quitGroup,groupList,breakGroup,groupMemberList,forbidSpeechList,upnickSet} from '../../api/api'
export default {
  name:'group_dialog',
  data(){
      return {
        briefText:'',
        noticeText:'',
        nickText:'',
        userText:'',
        overInnerDialog:false,
        removeBanDialog:false,
        banList:[],
        banItem:'',
      }
  },
  components: {
    'el-dialog':Dialog,
  },
  watch:{
    '$store.state.choosedGroupId'(){
      this.briefText = this.$store.state.groupRemarks
      this.noticeText = this.$store.state.groupNotice
      this.nickText = this.$store.state.groupName
    },
    '$store.state.groupMembersList'(){
      if(this.$store.state.choosedGroupId){
        // this.getForbidSpeechList()
      }
    }
  },
  mounted(){

  },
  methods:{
    cancelDialog(){
      this.propData.briefDialogState = false
      this.propData.noticeDialogState = false
      this.propData.nickDialogState = false
      this.propData.banDialogState = false
      this.propData.deleteDialogState = false
      this.propData.quitDialogState = false
      this.propData.breakDialogState = false
      this.propData.overDialogState = false
      this.propData.banMembersDialogState = false
      this.propData.removeBanDialogState = false
      this.$emit('changePropData',this.propData)
    },
    //修改群基本信息
    changeGroupMsg(data,callback){
      upNoticeRemarks(data)
      .then(res => {
        callback&&callback()
        this.cancelDialog()
        Message({
          message: '修改成功',
          type: 'success'
        })
      })
    },
    //修改群简介
    saveBrief(){
      let params = {
        roomId:this.$store.state.choosedGroupId,
        remarks:this.briefText
      }
      this.changeGroupMsg(params,()=>{
        this.$store.commit('changeGroupBase',{groupRemarks:this.briefText})
      })
    },
    //获取禁言列表
    getForbidSpeechList(){
      let params={
        roomId:this.$store.state.choosedGroupId,
        nickName:this.userText
      }

      forbidSpeechList(params)
      .then(res => {
        this.banList = res.data.data.list
      })
    },
    //修改群公告
    saveNotice(){
      let params = {
        roomId:this.$store.state.choosedGroupId,
        roomNotice:this.noticeText
      }
      this.changeGroupMsg(params,()=>{
        this.$store.commit('changeGroupBase',{groupNotice:this.noticeText})
      })
    },
    //修改群昵称
    saveGroupName(){
      let params = {
        roomId:this.$store.state.choosedGroupId,
        roomName:this.nickText
      }
      this.changeGroupMsg(params,()=>{
        this.$store.commit('changeGroupBase',{groupName:this.nickText})
      })
    },
    //解除禁言
    removeBan(){
      let params = {
        userId:this.banItem.userId,
        roomId:this.banItem.roomId,
        forbidSpeech:-1
      }
      upnickSet(params)
      .then(response => {
        let params = {
            pageNo:1,
            pageSize:500,
            roomName:this.$store.state.groupName,
            roomId:this.$store.state.choosedGroupId
        }
        //重新加载群成员列表
        groupMemberList(params)
        .then(res => {
          this.$store.commit('getGroupMembersList',res.data.data.list)
        })
        this.cancelRemoveBanDialog()
        Message({
          message: '取消禁言成功',
          type: 'success'
        })
      })
    },
    //打开转让群确认弹窗
    openOverInnerDialog(){
      this.overInnerDialog = true
    },
    //关闭转让群确认弹窗
    cancelOverInnerDialog(){
      this.overInnerDialog = false
    },
    //打开解除禁言成员
    openRemoveBanDialog(item){
      console.log(item)
      this.banItem = item
      this.removeBanDialog = true
    },
    cancelRemoveBanDialog(){
      this.removeBanDialog = false
    },
    //退出群
    exitGroup(){
      let params = {
        userId:this.$store.state.user.id,
        roomId:this.$store.state.choosedGroupId
      }
      quitGroup(params)
      .then(res => {
        let params1 = {
            pageNo:1,
            pageSize:500,
            masterId:this.$store.state.user.id
        }
        //加载群聊列表
        groupList(params1)
        .then(res => {
            var list = res.data.data.list
            this.$store.commit('getGroupList',list)
        })
        this.cancelDialog()
        Message({
          message: '退出群成功',
          type: 'success'
        })
      })
    },
    //移出群聊
    deleteGroupMember(){
      console.log(this.propData)
      let params = {
        userId:this.propData.chooseItem.userId,
        roomId:this.propData.chooseItem.roomId,
      }
      quitGroup(params)
      .then(response => {
        let params1 = {
            pageNo:1,
            pageSize:500,
            roomName:this.$store.state.groupName,
            roomId:this.$store.state.choosedGroupId
        }
        //加载群成员列表
        groupMemberList(params1)
        .then(res => {
          this.$store.commit('getGroupMembersList',res.data.data.list)
        })
        this.cancelDialog()
        Message({
          message: '移出群聊成功',
          type: 'success'
        })
      })
    },
    //解散群
    dismissGroup(){
      let params = {
        roomId:this.$store.state.choosedGroupId
      }
      breakGroup(params)
      .then(response => {
        let params1 = {
            pageNo:1,
            pageSize:500,
            masterId:this.$store.state.user.id
        }
        //加载群聊列表
        groupList(params1)
        .then(res => {
            var list = res.data.data.list
            this.$store.commit('getGroupList',list)
        })
        this.cancelDialog()
        Message({
          message: '解散群成功',
          type: 'success'
        })
      })
    }
  },
  props:{
    propData:Object
  }
}
</script>

<style scoped>
.dialog_title{
  display: flex;
  align-items: center;
  line-height: 42px;
  padding: 0 25px 0 30px;
}
.input_box{
  border: 1px solid #f1f1f1;
  padding: 6px 7px 3px;
  border-radius: 2px;
  margin: 0 30px 30px;
  height: 180px;
}
.input_text{
  outline: none;
  border: none;
  resize: none;
  font-size: 12px;
  width: 100%;
  padding: 0;
  height: 150px;
}
.input_num{
  color: #999;
  float: right;
  padding-right: 7px;
  font-size: 12px;
}
.btns_box{
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 30px;
}
.primarybtn.default{
  margin-left: 0px;
}
.primarybtn{
  margin-left:46px;
}
.input_box.single_line_box{
  display: flex;
  align-items: center;
  height: 30px;
}
.input_text.single_line_input{
  flex:1;
  height: 100%;
}
.ban_time{
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding:0 25px;
  height: 50px;
  cursor: pointer;
}
.time_duration{
  font-size: 16px;
  color: #333;
}
.choose_icon{
  width:18px;
}

.dialog_tips{
  line-height: 20px;
  padding: 46px 0 37px;
  text-align: center;
}
.little_tips{
  font-size: 12px;
}

.over_group_top{
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding:0 20px;
}
.over_group_title{
  height: 60px;
  line-height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.close{
  width: 24px;
  height: 24px;
  cursor: pointer;
}
.over_group_search{
  display: flex;
  justify-content: center;
  align-items: center;
  padding:0 20px;
  margin-bottom:8px;
}
.search_icon{
  width: 20px;
  height: 20px;
  padding-left:20px;
}
.search_input{
  margin-left:2px;
  font-size:13px;
  border:none;
  width:250px;
}

.group_members_box{
  overflow: auto;
  height:372px;
}
.group_members_line{
  display: flex;
  align-items: center;
  height: 54px;
  padding: 10px 33px;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
}
.group_members_line:hover{
  background: #edeff3;
}
.group_member_avatar{
  width:34px;
  height:34px;
}
.group_member_name{
  font-size:14px;
  color:#333;
  margin-left:10px;
}

.dialog_top{
  display: flex;
  align-items: center;
  justify-content: space-between;
  line-height: 42px;
  padding: 0 25px 0 30px;
}
.cancel_dialog_btn{
  width:24px;
  height:24px;
  cursor: pointer;
}
.user_search{
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 25px 0 30px;
}
.user_search .search_icon{
  padding:0px 8px;
}
.search_input{
  min-width:280px;
  color: #428dfc;
  line-height: 30px;
  border:none;
}
.user_list{
  padding:10px 30px 20px;
  overflow: auto;
  height: 450px;
}
.user_box{
  padding: 10px;
  display: flex;
  align-items: center;
}
.user_avatar{
  width:40px;
  height:40px;
}
.user_text{
  flex:1;
  margin:0 10px;
}
.user_nick{
  font-size: 14px;
  color: #333;
}
.remove_ban{
  display: flex;
  align-items: center;
  cursor: pointer;
}
.remove_time{
  font-size: 14px;
  color: #333;
}
.remove_icon{
  width: 18px;
  height: 18px;
  margin-left: 10px;
}
</style>